package com.uwntek.struts2.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.base.Pagination.Pagination;
import com.opensymphony.xwork2.ActionSupport;
import com.system.pojo.Person;
import com.system.service.IPersonService;
import com.system.struts2.util.PersonInfo;
import com.system.struts2.util.PersonUtil;
import com.uwntek.pojo.Projectmanage;
import com.uwntek.service.IProjectManageService;

public class ProjectManageAction extends ActionSupport{
	private Person person;
	private IPersonService personSer;
	private Projectmanage projectmanage;
	private IProjectManageService<Projectmanage> projectManageSer;
	public List<Projectmanage> projectList = new ArrayList<Projectmanage>();
	public List<Projectmanage> projectListNew = new ArrayList<Projectmanage>();
	
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String imagePath;
	
	HttpServletRequest request;
	HttpServletResponse response;
	private Pagination pagination;
	private boolean flag =false;
	public String list()
	{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
		int totalCount = projectManageSer.getTotalCount(
				" select count(w) from Projectmanage w " + " where w.person = "
						+ personInfo.getId(), null);
		// 生成分页工具条
		pagination = new Pagination(request, response);
		pagination.setRecordCount(totalCount);
		projectList= projectManageSer.list("from Projectmanage as p where p.person ="+personInfo.getId(),pagination
				.getFirstResult(), pagination.getPageSize(),null);
//		Iterator<Projectmanage> it = projectListNew.iterator();
//		while(it.hasNext()){
//			Projectmanage projectManageObject = it.next();
//			try{
//			projectManageObject.setProjectPath(java.net.URLDecoder.decode(projectManageObject.getProjectPath(), "utf-8"));
//			}catch (IOException e) {
//				// TODO: handle exception
//				e.printStackTrace();
//			}
//			projectList.add(projectManageObject);
//		}
		return "list";
	}
	public String save() throws Exception
	{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
		person = (Person) personSer.find(Person.class, personInfo.getId());
		projectmanage.setPerson(person);
		projectmanage.setDateCreated(new Date());
		projectmanage.setDateLastActive(new Date());
		
		copy(this.getUpload(), getImageFile());
		//String fileName =java.net.URLEncoder.encode(imagePath+this.uploadFileName,"utf-8");// new String((imagePath+this.uploadFileName).getBytes(),"gbk");
		
		projectmanage.setProjectPath(imagePath+this.uploadFileName);
		projectManageSer.create(projectmanage);
		return list();
	}
	public String load()
	{
		projectmanage = projectManageSer.find(Projectmanage.class, projectmanage.getId());
		return "load";
	}
	public String modify()
	{
		Projectmanage project = projectManageSer.find(Projectmanage.class, projectmanage.getId());
		project.setDateLastActive(new Date());
		project.setProjectCode(projectmanage.getProjectCode());
		project.setProjectContent(projectmanage.getProjectContent());
		copy(this.getUpload(), getImageFile());
		project.setProjectPath(imagePath+this.uploadFileName);
		projectManageSer.save(project);
		return list();
	}
	
	public File getImageFile() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		PersonInfo personInfo = PersonUtil.getPersonInfo(request, response);
		person = (Person) personSer.find(Person.class, personInfo.getId());

			imagePath =ServletActionContext.getServletContext().getRealPath(
			"/")
			+ "file/工程包/"
					+ person.getName()
					+ "/";
		File dir = new File(imagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File imageFile = new File(dir, new String(uploadFileName));
		imageFile.deleteOnExit();
		try {
			imageFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imageFile;
	}

	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				byte[] buffer = new byte[70 * 1024* 1024];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	
	public IPersonService getPersonSer() {
		return personSer;
	}
	public void setPersonSer(IPersonService personSer) {
		this.personSer = personSer;
	}
	public Projectmanage getProjectmanage() {
		return projectmanage;
	}
	public void setProjectmanage(Projectmanage projectmanage) {
		this.projectmanage = projectmanage;
	}
	public IProjectManageService<Projectmanage> getProjectManageSer() {
		return projectManageSer;
	}
	public void setProjectManageSer(
			IProjectManageService<Projectmanage> projectManageSer) {
		this.projectManageSer = projectManageSer;
	}
	public List<Projectmanage> getProjectList() {
		return projectList;
	}
	public void setProjectList(List<Projectmanage> projectList) {
		this.projectList = projectList;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getImagePath() {
		return imagePath;
	}
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
	public Pagination getPagination() {
		return pagination;
	}
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	

}
